package Robots;

import java.util.ArrayList;

import Main.Game;
import Other.Field;
import Units.Robot;
import Units.Unit;
import Units.UnitName;

import jgame.JGObject;
import jgame.JGPoint;
import jgame.JGTimer;

public class Robot4 extends Robot {
	public static int imageHeight = 68, imageWidth = 28;
	
	private final static String stringStandingDown = "robot4_down",
								stringGoDown	   = "robot1animgo_down",
								stringStopDown	   = "robot1animstop_down",
								stringMoveDown	   = "robot1animmove_down";
	
	private int state = 0;
	
	public Robot4(String name, boolean unique, double posX, double posY, int cid){
		super(name, unique, posX, posY, cid, stringStandingDown);
		setSteps(2);
	}
	
	// move methods
	@Override
	public boolean canMove(Field field) {
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( UnitName.isRobot(Game.board.getUnitName(field.i, field.j)) ) return false;

		if (getSteps() == 1 || getSteps() == 2){
			for (int dir = 0; dir < 6; dir++){
				Field current = new Field(this.getField());
				
				if (getSteps() == 1 && current.compare(field)){
					return true;
				}
				
				int di = Field.adjacentFields[dir][0];
				int dj = Field.adjacentFields[dir][1];
				
				current.i += di; current.j += dj;
				if (getSteps() == 1 && current.compare(field)){
					return true;
				}
				
				current.i += di; current.j += dj;
				if (getSteps() == 2 && current.compare(field)){
					return true;
				}
				
				current.i -= di; current.j -= dj;
				
				di = Field.adjacentFields[(dir + 1) % 6][0];
				dj = Field.adjacentFields[(dir + 1) % 6][1];
				
				current.i += di; current.j += dj;
				
				if (getSteps() == 2 && current.i == field.i && current.j == field.j){
					return true;
				}
			}
		}
		
		return false;
	}
	
	public void move() {
		switch (getState()){
			case 0:
				setGraphic(stringStandingDown);
				break;
			case 1:
				setGraphic(stringGoDown);
				break;
			case 2:
				setGraphic(stringMoveDown);
				JGPoint p = Game.board.getRealCoordinates(getTargetField());
				
				if (Game.board.distance(p, new JGPoint((int)getLastX() + this.imageWidth/2, (int)getLastY() + this.imageHeight/2)) < 5){
					xspeed = yspeed = 0;
					setState(3);
					new JGTimer(5, true){

						@Override
						public void alarm() {
							setState(0);
							Game.board.setAnimation(false);
							Field f = Robot4.this.getField();
							Field t = Robot4.this.getTargetField();
							
							Game.board.moveFromTo(f, t);
							Robot4.this.setField(Robot4.this.getTargetField());
							if (Robot4.this.getSteps() == 1) 
								Game.game.checkAllowed(true);
						}
					};
				}
				break;
			case 3:
				setGraphic(stringStopDown);
				break;
			default: setGraphic(stringStandingDown);
		}		
	}
	
	public void resetSteps(){ setSteps(2); }

	@Override
	public ArrayList<Field> whereAttack(Field field) {
		// TODO Auto-generated method stub
		//return null;
		ArrayList<Field> ret = new ArrayList<Field>();
		ret.clear();
		return ret;
	}
	
	@Override
	public boolean canAttack(Field field) {
		// TODO Auto-generated method stub
		return false;
	}
}
